home *** CD-ROM | disk | FTP | other *** search
/ F1 Licenseware / F1 Licenseware - Volume 1.iso / disks / 050a.dms / 050a.adf / TEXTS / chapter23.txt < prev    next >
Text File  |  1992-02-26  |  6KB  |  159 lines

  1.                      The Absolute Beginners Guide To Amos
  2.                     -------------------------------------
  3.                              Chapter Twenty Three
  4.                             ----------------------
  5. After that bit of fun with SHIFT we need to get back to the nitty gritty
  6. commands that you are going to need sooner or later. In this chapter we
  7. shall be taking a further look at strings.  We have already met the normal
  8. string function ($) as well as UPPER$ and LOWER$, but there is a lot more to
  9. this subject. Load up Example23.Amos and take a quick look at the code.
  10. It is a simple Word Guessing game that makes use of the INSTR command.
  11.  
  12. OK the program isn't great in it's own right but it is a good example of how
  13. useful INSTR can be when manipulating strings.  Let's take a look at INSTR.
  14.  
  15. Put simply INSTR is used for finding a character or characters inside a
  16. string of characters. An example:
  17.  
  18.  
  19. REM A$ is the source string
  20. '--------------------------
  21. A$="I JUST WALKED UP TO THE CHAP AND SAID HELLO"
  22.  
  23. REM B$ is the string to matched
  24. '------------------------------
  25. B$="I"
  26.  
  27. REM This is where the check is performed
  28. '----------------------------------------
  29. X=INSTR(A$,B$)
  30.  
  31.  
  32. If A$ does contain B$ (Which in this case it does) then the variable X will
  33. hold the position in characters of it's place in A$. So in this case X will
  34. return the value 1 because the I is the first letter in A$.
  35.  
  36. If we had searched for a word like UP that would return the position of U
  37. (character 15) in A$. If X was to return the value of 0 then this would 
  38. indicate that no match was found. 
  39. Taking this example one step further we could add a few lines to the above 
  40. program like this:
  41.  
  42. IF X=0 THEN PRINT "NO MATCH FOUND"
  43. IF X<>0 THEN PRINT "FOUND AT POSITION ";X
  44.  
  45. Initially INSTR appears to be a bit daunting but once you have used it a few
  46. times it will be an easy to use and powerful function worth knowing about.
  47. INSTR is definitely one for your note book.
  48.  
  49. Now go back to Example23.Amos and study the INSTR parts of it.
  50.  
  51.  
  52. STRING$
  53. -------
  54. The only other command/function used in Example23.Amos that we haven't looked
  55. at yet is the STRING$ function. This is the offending line: 
  56.  
  57. Pen 4 : Locate 0,3 : Print String$("-",L)
  58.  
  59. We know all about PEN, LOCATE and PRINT so we won't discuss them.
  60.  
  61. STRING$ allows us to duplicate a string as many times as we want.
  62. A simple example could be this.
  63.  
  64. Let us say we want to PRINT a line of dashes (-) across the screen. 
  65. The screen is in Lowres so it is 40 characters wide. This means we will need
  66. 40 of them. We could do this:
  67.  
  68. PRINT"----------------------------------------"
  69.  
  70. Which is ugly and takes up more memory than this:
  71.  
  72. PRINT STRING$("-",40)
  73.  
  74. We are simply telling Amos to duplicate the - 40 times and print it. 
  75. A lot more professional and compact don't you think?
  76.  
  77. Back to the line in example23.Amos. The only difference there is that we are
  78. using the variable L for the amount of duplications. 
  79. The reason we are using a variable is that the amount of dashes (-) VARY 
  80. according to the LENgth of the hidden word to be found by the user. 
  81. We have already covered the LEN function in chapter 16 so take a look back 
  82. there if you are not sure.
  83.  
  84. We have now covered every command and function used in Example23.Amos but 
  85. there are a few more string functions I would like to show you while we are 
  86. on the subject.
  87.  
  88.  
  89. SPACE$
  90. ------
  91. SPACE$ is a nice place to start. SPACE$ is quite similar to STRING$ in the
  92. respect that it can PRINT a given amount of characters. The difference here
  93. is that SPACE$ only deals with SPACES. Here is an example use for SPACE$:
  94.  
  95. Let us say you have written a program and you have reserved a line on the
  96. screen to print up text messages etc. Now some of the messages are going to
  97. be longer than other messages so you will need to erase the line without 
  98. disturbing anything else on the screen. You could do it like this:
  99.  
  100. LOCATE 0,3: PRINT "                                       "
  101.  
  102. Or you could use this:
  103.  
  104. LOCATE 0,3: PRINT SPACE$(40)
  105.  
  106. Alternatively another good example would be:
  107.  
  108. PRINT "3 SPACES COMING UP";SPACE$(3);"SEE!"
  109.  
  110. I think that speaks for itself.
  111.  
  112.  
  113. FLIP$
  114. -----
  115. FLIP$ is a function I have never found a use for but I will tell you about it
  116. anyway.
  117.  
  118. As you have probably guessed FLIP$ literally FLIPs your string:
  119.  
  120. PRINT FLIP$("FLIP AMOS)
  121.  
  122. Would come out SOMA PILF and that's it really.
  123.  
  124. Following are three more string functions that will be of a lot more use to
  125. you in everyday programming.
  126.  
  127. LEFT$, RIGHT$ and MID$
  128. ----------------------
  129. We will first take a look at LEFT$:
  130.  
  131. A$="SOME TEXT"
  132. PRINT LEFT$ (A$,4)
  133.  
  134. The above program will PRINT the word SOME on the screen. This is what LEFT$
  135. does it looks at a string starting from the LEFTmost side and reads the 
  136. specified number of characters (4 in this case) from the source string (in
  137. this case A$)
  138.  
  139. If the program read LEFT$(a$,7) it would produce SOME T (remember the space
  140. is counted as well) Please load Example23_1.amos for a short demo.
  141.  
  142. RIGHT$ is the same but reads from the rightmost character backward through
  143. the string. See also Example23_1.Amos
  144.  
  145. MID$ is very useful, it is a combination of LEFT$ and RIGHT$ really.
  146.  
  147. A$="SOME TEXT"
  148. PRINT MID$ (A$,6,4)
  149.  
  150. Would produce TEXT on the screen. The 6 is the character you wish to start
  151. with (called the offset) and the 4 is how many characters long you would
  152. like it. Example23_1.Amos should make it a bit clearer.
  153.  
  154. OK string functions are pretty heavy going so we will leave it there for now
  155. and move on to pastures new.
  156.  
  157.                               End of Chapter 23
  158.                               ^^^^^^^^^^^^^^^^^
  159.